package com.cjlgb.design.upms.service.impl;

import com.cjlgb.design.common.core.bean.ResultBean;
import com.cjlgb.design.common.mybatis.service.impl.BaseServiceImpl;
import com.cjlgb.design.upms.api.entity.SysRole;
import com.cjlgb.design.upms.channel.SysRoleChannel;
import com.cjlgb.design.upms.mapper.SysRoleMapper;
import com.cjlgb.design.upms.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author WFT
 * @date 2019/7/31
 * description: 系统角色业务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper,SysRole> implements SysRoleService {

    private final SysRoleChannel roleChannel;

    @Override
    public ResultBean batchDelete(List<Long> idList) {
        if (this.removeByIds(idList)){
            //  异步删除与菜单账号的关联
            this.roleChannel.output().send(
                    MessageBuilder.withPayload(idList)
                            .setHeader("method", "delete").build()
            );
            return ResultBean.ok();
        }
        return ResultBean.error("删除失败,角色Id可能不存在");
    }

    @Override
    public ResultBean changeAuthorities(SysRole role) {
        Assert.notNull(this.getById(role.getId()),"角色Id不存在");
        //  异步更改角色的权限
        this.roleChannel.output().send(
                MessageBuilder.withPayload(role)
                        .setHeader("method", "changeAuthorities").build()
        );
        return ResultBean.ok();
    }
}
